package com.niiwoo.civet.user.service.duboo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.asset.cardniu.service.loan.LoanDubboService;
import com.niiwoo.civet.user.dao.entity.UserBlackList;
import com.niiwoo.civet.user.dao.entity.UserLibraLoanLimit;
import com.niiwoo.civet.user.dto.UserForbidDubboInfoDTO;
import com.niiwoo.civet.user.dto.request.AddUserLoanLimitRequestDTO;
import com.niiwoo.civet.user.dto.response.UserBlackListInfoDTO;
import com.niiwoo.civet.user.dto.response.UserLibraLoanLimitDTO;
import com.niiwoo.civet.user.dto.response.ValidUserForbidResponseDTO;
import com.niiwoo.civet.user.enums.BlackEnum;
import com.niiwoo.civet.user.enums.LibraLoanLimitEnum;
import com.niiwoo.civet.user.enums.ProductTypeEnum;
import com.niiwoo.civet.user.enums.UserForbidTypeEnum;
import com.niiwoo.civet.user.service.UserForbidDubboService;
import com.niiwoo.civet.user.service.local.LibraLoanLimitService;
import com.niiwoo.civet.user.service.local.UserBlackListLocalService;
import com.niiwoo.civet.user.service.local.UserForbidService;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author: wuke
 * @Description:
 * @Date: Created in 17:40 2017/11/7
 */
@Slf4j
@Service(version = "1.0.0")
public class UserForbidDubboServiceImpl implements UserForbidDubboService {

    @Autowired
    private UserForbidService userForbidService;

    @Autowired
    private UserBlackListLocalService blackListLocalService;

    @Autowired
    private LibraLoanLimitService libraLoanLimitService;

    @Reference(version = "1.0.0")
    private LoanDubboService loanDubboService;

    @Autowired
    PrivacyMasks privacyMasks;

    @Override
    public ValidUserForbidResponseDTO queryValidUserForbidInfo(String userId,UserForbidTypeEnum userForbidTypeEnum) {
        return userForbidService.queryValidUserForbidInfo(userId,userForbidTypeEnum);
    }

    @Override
    public void validUserForbidInfo(String userId, UserForbidTypeEnum userForbidTypeEnum){
        userForbidService.validUserForbidInfo(userId,userForbidTypeEnum);
    }

    @Override
    public void saveUserForbidInfo(UserForbidDubboInfoDTO userForbidDubboInfoDTO) {
        userForbidService.saveUserForbidInfo(userForbidDubboInfoDTO);
    }

    @Override
    public UserBlackListInfoDTO getUserBlackListInfo(String userId) {
        UserBlackListInfoDTO userBlackListInfoDTO = null;
        List<UserBlackList> userBlackListList = blackListLocalService.getUserBlackList(userId);
        if(userBlackListList != null && userBlackListList.size() > 0) {
            UserBlackList userBlackList = userBlackListList.get(0);
            // 黑名单优先
           for(UserBlackList blackList : userBlackListList) {
               if(BlackEnum.BlackType.BLACK_LIST.getType() == blackList.getListType()) {
                   userBlackList = blackList;
                   break;
               }
           }
            userBlackListInfoDTO = new UserBlackListInfoDTO();
            userBlackListInfoDTO.setListType(Integer.valueOf(userBlackList.getListType()));
            userBlackListInfoDTO.setUserType(Integer.valueOf(userBlackList.getUserType()));
            userBlackListInfoDTO.setUserSource(Integer.valueOf(userBlackList.getUserSource()));
            userBlackListInfoDTO.setBeginTime(userBlackList.getBeginTime());
            userBlackListInfoDTO.setEndTime(userBlackList.getEndTime());
            userBlackListInfoDTO.setAddReason(userBlackList.getAddReason());
            userBlackListInfoDTO.setBlackListDesc(userBlackList.getBlackListDesc());
        }

        return userBlackListInfoDTO;
    }

    @Override
    public UserLibraLoanLimitDTO getUserLibraLoanLimit(String userId, ProductTypeEnum productTypeEnum) {
        UserLibraLoanLimit userLibraLoanLimit = libraLoanLimitService.getUserLibraLoanLimit(userId, productTypeEnum);
        if(userLibraLoanLimit != null) {
            UserLibraLoanLimitDTO userLibraLoanLimitDTO = new UserLibraLoanLimitDTO();
            userLibraLoanLimitDTO.setUserId(userId);
            userLibraLoanLimitDTO.setLimitType(userLibraLoanLimit.getLimitType());
            userLibraLoanLimitDTO.setHitTime(userLibraLoanLimit.getHitTime());
            userLibraLoanLimitDTO.setLimitedTime(userLibraLoanLimit.getLimitedTime());
            return userLibraLoanLimitDTO;
        }
        return null;
    }

    @Override
    public UserLibraLoanLimitDTO getUserLibraLoanLimit(String userId, Byte projectType) {
        UserLibraLoanLimit userLibraLoanLimit = libraLoanLimitService.getUserLibraLoanLimit(userId, projectType);
        if(userLibraLoanLimit != null) {
            UserLibraLoanLimitDTO userLibraLoanLimitDTO = new UserLibraLoanLimitDTO();
            userLibraLoanLimitDTO.setUserId(userId);
            userLibraLoanLimitDTO.setLimitType(userLibraLoanLimit.getLimitType());
            userLibraLoanLimitDTO.setHitTime(userLibraLoanLimit.getHitTime());
            userLibraLoanLimitDTO.setLimitedTime(userLibraLoanLimit.getLimitedTime());
            return userLibraLoanLimitDTO;
        }
        return null;
    }

    @Override
    public void addUserLibraLoanLimit(AddUserLoanLimitRequestDTO addUserLoanLimitRequestDTO){
        libraLoanLimitService.addLibraLoanLimit(addUserLoanLimitRequestDTO.getUserId(), LibraLoanLimitEnum.getEnumByType(addUserLoanLimitRequestDTO.getLimitType().byteValue()), addUserLoanLimitRequestDTO.getProductTypeEnum(), addUserLoanLimitRequestDTO.getLimitDays());
    }
    
    @Override
    public void addUserLibraLoanLimitAllType(AddUserLoanLimitRequestDTO addUserLoanLimitRequestDTO){
    	ProductTypeEnum[] allTypes = ProductTypeEnum.values();
    	for (ProductTypeEnum productType : allTypes) {
    		libraLoanLimitService.addLibraLoanLimit(addUserLoanLimitRequestDTO.getUserId(), LibraLoanLimitEnum.getEnumByType(addUserLoanLimitRequestDTO.getLimitType().byteValue()), productType, addUserLoanLimitRequestDTO.getLimitDays());
    	}
    }

    /**
     * 卡牛用户黑名单的列表个数
     * @param userId
     * @return
     */
    @Override
    public int getUserBlackList(String userId) {
        log.info("卡牛用户黑名单的列表个数{}",userId);
        List<UserBlackList> userBlackLists =  blackListLocalService.getUserBlackList(userId);
        log.info("用户黑名单列表userId = {},userBlackLists={}",userId,userBlackLists.size());
        if(!userBlackLists.isEmpty()){
            return userBlackLists.size();
        }
        return 0;
    }

    @Override
    public ValidUserForbidResponseDTO cardNiuvalidUserForbidInfo(Long id,String userId, UserForbidTypeEnum userForbidTypeEnum){
        log.info("卡牛行为限制id= {},userId = {},userForbidTypeEnum = {}",id,userId, JSONObject.toJSON(userForbidTypeEnum));
        return userForbidService.cardNiuvalidUserForbidInfo(userId,userForbidTypeEnum);
    }

    @Override
    public boolean isHitBlackList(String mobile,String idCard) {
        List<UserBlackList> userBlackListByIdCard=blackListLocalService.getUserBlackListByIdCard(privacyMasks.encryptPrivacy(idCard));
        List<UserBlackList> userBlackListByMobileNo=blackListLocalService.getUserBlackListByMobileNo(privacyMasks.encryptPrivacy(mobile));

        return (userBlackListByIdCard == null || userBlackListByIdCard.size() == 0) && (userBlackListByMobileNo == null || userBlackListByMobileNo.size() == 0);
    }

}
